<?php

use console\db\channels\TransferTableMigration;
use yii\helpers\Json;

/**
 * 圈子帖子内容表迁移
 * 
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class m300020_000004_t_sns_dynamic_content extends TransferTableMigration {

    /**
     * @inheritdoc
     */
    public $sourceDb = 'olddb';

    /**
     * @inheritdoc
     */
    public function getTransferOptions() {
        return [
            'table' => [
                'sns_dynamic_content' => 'sns_group_thread_data',
            ],
            'columns' => [
                'dynamic_id' => 'thread_id',
                'body' => function($data) {
                    $content = trim(strip_tags($data['content']));
                    if ($content) {
                        return $content;
                    }
                    return null;
                },
                'attachs' => function($data) {
                    if ($data['content']) {
                        $pattern = "/<[img].*?src=[\'|\"](.*?(?:[\.gif|\.jpg|\.png||\.jpeg]))[\'|\"].*?[\/]?>/i";
                        preg_match_all($pattern, $data['content'], $match);
                        if (isset($match[1]) && !empty($match[1])) {
                            return Json::encode($match[1]);
                        }
                    }
                    return null;
                },
            ],
        ];
    }

}
